package com.ruoyi.web.controller.community;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BbsReport;
import com.ruoyi.community.service.IBbsReportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛举报Controller
 * 
 * @author Gaotian
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/system/report")
public class BbsReportController extends BaseController
{
    @Autowired
    private IBbsReportService bbsReportService;

    /**
     * 查询论坛举报列表
     */
//    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(BbsReport bbsReport)
    {
        startPage();
        List<BbsReport> list = bbsReportService.selectBbsReportList(bbsReport);
        return getDataTable(list);
    }

    @GetMapping("/heartList")
    public TableDataInfo heartList(BbsReport bbsReport)
    {
        startPage();
        List<BbsReport> list = bbsReportService.selectHeartReportList(bbsReport);
        return getDataTable(list);
    }

    /**
     * 导出论坛举报列表
     */
//    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "论坛举报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BbsReport bbsReport)
    {
        List<BbsReport> list = bbsReportService.selectBbsReportList(bbsReport);
        ExcelUtil<BbsReport> util = new ExcelUtil<BbsReport>(BbsReport.class);
        return util.exportExcel(list, "论坛举报数据");
    }

    /**
     * 获取论坛举报详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bbsReportService.selectBbsReportById(id));
    }

    /**
     * 新增论坛举报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "论坛举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BbsReport bbsReport)
    {
        Long userid = SecurityUtils.getUserID();
        Long communityid = Long.valueOf(SecurityUtils.getCommunityId());
        Long articleid = bbsReport.getArticleid();
        Integer fromsource = bbsReport.getFromsource();
        List<BbsReport> res = bbsReportService.selectBbsReportList(new BbsReport(articleid,userid, communityid,fromsource));
        if(res.size()>0){
            AjaxResult ajax = new AjaxResult();
            return ajax.error(300,"您已经举报过该帖子");
        }
        bbsReport.setCommunityid(communityid);
        bbsReport.setReporterid(userid);
        return toAjax(bbsReportService.insertBbsReport(bbsReport));
    }

    /**
     * 修改论坛举报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "论坛举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BbsReport bbsReport)
    {
        return toAjax(bbsReportService.updateBbsReport(bbsReport));
    }

    /**
     * 删除论坛举报
     */
//    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "论坛举报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bbsReportService.deleteBbsReportByIds(ids));
    }
}
